package com.example.myapplication.Style;

import IntentMessage.IntentActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import com.example.myapplication.MainActivity;
import com.example.myapplication.Media.MediaActivity;
import com.example.myapplication.Person;
import com.example.myapplication.R;
import java.io.InputStream;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

public class StyleActivity extends AppCompatActivity {
  @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.style_layout);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle("样式测试");
    setSupportActionBar(toolbar);
    ApplySaxButton();
    ApplyDomButton();
    ApplyPullButton();
    ApplyGenXmlButton();
  }

  @Override public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(0, 1, 0, "返回上一个Activity");
    menu.add(0, 2, 1, "跳转到下一个Activity");
    return true;
  }

  @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()) {
      case 1:
        BackToLastActivity();
        break;
      case 2:
        GoToNextActivity();
        break;
    }
    return true;
  }
  private void GoToNextActivity() {
    Intent intent = new Intent(this, MediaActivity.class);
    startActivity(intent);
  }
  private void BackToLastActivity() {
    Intent intent = new Intent(this, IntentActivity.class);
    startActivity(intent);

  }
  private void ApplySaxButton() {
    Button button = (Button) findViewById(R.id.sax_button);
    button.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        JumpSaxActivity();
      }
    });
  }
  private void ApplyDomButton() {
    Button button = (Button) findViewById(R.id.dom_button);
    button.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        JumpDomActivity();
      }
    });
  }
  private void ApplyPullButton() {
    Button button = (Button) findViewById(R.id.pull_button);
    button.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        JumpPullActivity();
      }
    });
  }
  private void ApplyGenXmlButton() {
    Button button = (Button) findViewById(R.id.pull_gen_button);
    button.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        GenXML();
      }
    });
  }
  private void JumpSaxActivity() {
    Intent intent = new Intent(this, PersonInfoActivity.class);

    ArrayList<Person> personArrayList=GetDataBySax();
    intent.putExtra("personList", personArrayList);
    intent.putExtra("title","Sax解析XML文件");
    startActivity(intent);

  }
  private void JumpDomActivity() {
    ArrayList<Person> personArrayList = GetDataByDom();
    Intent intent = new Intent(this, PersonInfoActivity.class);
    intent.putExtra("personList", personArrayList);
    intent.putExtra("title", "Dom解析XML文件");

    startActivity(intent);
  }
  private void JumpPullActivity() {
    ArrayList<Person> personArrayList=GetDataByPull();
    Intent intent = new Intent(this, PersonInfoActivity.class);
    intent.putExtra("personList", personArrayList);
    intent.putExtra("title", "Pull解析XML文件");

    startActivity(intent);
  }

  private ArrayList<Person> GetDataBySax() {
    ArrayList<Person> personList;
    try {
      SAXParserFactory factory=SAXParserFactory.newInstance();
      SAXParser parser= factory.newSAXParser();

      //etResources().getXml(R.xml.person_values)返回的是 XmlResourceParser对象
      //InputStream inputStream = (InputStream) getResources().getXml(R.xml.person_values);
      InputStream inputStream = getAssets().open("person_values.xml");
      SAXParserHandler handler=new SAXParserHandler();
      parser.parse(inputStream,handler);

      personList = handler.getPersonList();
      System.out.println(personList.size());

    } catch (Exception e) {
      throw new RuntimeException(e);
    }
    return personList;
  }

  private ArrayList<Person> GetDataByDom() {
    ArrayList<Person> personArrayList=new ArrayList<>();
    try {
      InputStream inputStream = getAssets().open("person_values.xml");
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      DocumentBuilder builder = factory.newDocumentBuilder();
      Document document = builder.parse(inputStream);

      Element rootElement = document.getDocumentElement();
      NodeList personList = rootElement.getElementsByTagName("person");
      for (int i = 0; i < personList.getLength(); i++) {
        Element personElement = (Element) personList.item(i);
        String name=personElement.getElementsByTagName("name").item(0).getTextContent();
        String job=personElement.getElementsByTagName("job").item(0).getTextContent();
        Person person = new Person(name, job);
        personArrayList.add(person);
      }
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
    return personArrayList;
  }

  private ArrayList<Person> GetDataByPull() {
    ArrayList<Person> personArrayList = new ArrayList<>();
    try {
      InputStream inputStream = getAssets().open("person_values.xml");
      XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
      XmlPullParser parser = factory.newPullParser();
      parser.setInput(inputStream, null);

      int eventType = parser.getEventType();

      String name = "";
      String job = "";
      while (eventType != XmlPullParser.END_DOCUMENT) {
        String tagName = parser.getName();

        switch (eventType) {
          case XmlPullParser.START_TAG:
            if (tagName.equalsIgnoreCase("name")) {
              name=parser.nextText();
            } else if (tagName.equalsIgnoreCase("job")) {
              job=parser.nextText();
            }
            break;

          case XmlPullParser.END_TAG:
            if (tagName.equalsIgnoreCase("person")) {
              Person person = new Person(name, job);
              personArrayList.add(person);
            }
            break;
        }
        eventType= parser.next();
      }
    } catch (Exception e) {
      throw new RuntimeException(e);
    }

    return personArrayList;
  }
  private void GenXML() {
    ArrayList<Person> personArrayList = new ArrayList<>();
    personArrayList.add(new Person("天长地久", "图书馆管理员"));
    personArrayList.add(new Person("阿伟", "华莱士店长"));
    XmlGenerator xmlGenerator=new XmlGenerator();
    xmlGenerator.GeneratePersonXml(this,personArrayList);
    Toast.makeText(getApplicationContext(),"生成完成...",Toast.LENGTH_SHORT).show();
  }

}
